package com.houtai.service.user.info;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.TdUserInfo;
import com.houtai.bean.TsJzDict;
import com.houtai.bean.User;
import com.houtai.bean.dto.avtivity.works.UserIndexWorksDto;
import com.houtai.bean.dto.user.info.TdUserInfoInsertDto;
import com.houtai.bean.dto.user.info.TdUserInfoUpdateDto;
import com.houtai.bean.vo.activity.works.TdActivityWorksList;
import com.houtai.bean.vo.activity.works.TdActivityWorksListVo;
import com.houtai.bean.vo.user.UserIndexDetail;
import com.houtai.bean.vo.user.UserIndexDetailVo;
import com.houtai.bean.vo.user.info.TdUserInfoDetailVo;
import com.houtai.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoService {
    private static Logger logger = Logger.getLogger(UserInfoService.class);
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdUserInfoDao tdUserInfoDao;
    @Autowired
    private TsJzDictDao tsJzDictDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private TdBlackDao tdBlackDao;
    @Autowired
    private TdActivityWorksDao tdActivityWorksDao;
    /**
     * @Purpose:    用户主页作品列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdActivityWorksListVo
     */
    public TdActivityWorksListVo userWorksList(UserIndexWorksDto dto) {
        String logInfo = this.getClass().getName() + ":userWorksList:";
        TdActivityWorksListVo vo = new TdActivityWorksListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = userDao.findUserId(dto.getUser_id());
            if (user != null) {
                if (user.getIs_auth() == 0 && user.getUser_type() == 1) {
                    String orderBy = "works_id DESC";
                    PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
                    List<TdActivityWorksList> lists = tdActivityWorksDao.findTdActivityWorksListByUserId(dto.getUser_id());
                    PageInfo<TdActivityWorksList> list = new PageInfo<>(lists);
                    if (list.getTotal() > 0) {
                        vo.setCurrent_page(dto.getCurrent_page());
                        vo.setList(list.getList());
                        vo.setPage_size(list.getPageSize());
                        vo.setTotal_record(list.getTotal());
                        vo.setPages(list.getPages());
                        vo.setMark("0");
                        vo.setTip("查询成功");
                    } else {
                        vo.setMark("1");
                        vo.setTip("无作品");
                    }
                } else {
                    vo.setMark("1");
                    vo.setTip("该用户未实名认证");
                }

            } else {
                vo.setMark("1");
                vo.setTip("用户不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            vo.setMark("-1");
            vo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    用户主页
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized UserIndexDetailVo userIndex(int user_id) {
        String logInfo =  ":userIndex:";
        logger.info("======" + logInfo + "begin======");
        UserIndexDetailVo vo = new UserIndexDetailVo();
        try {
            User user = userDao.findUserId(user_id);
            if (user != null) {
                if (user.getIs_auth() == 0 && user.getUser_type() == 1) {
                    TdUserInfo dto = tdUserInfoDao.findTdUserInfoById(user.getUser_id());
                    UserIndexDetail tdUserInfo = new UserIndexDetail();

                    tdUserInfo.setHometown(dto.getHometown());
                    tdUserInfo.setNick_name(dto.getNick_name());
                    tdUserInfo.setProfession(dto.getProfession());
                    tdUserInfo.setSex(dto.getSex());
                    tdUserInfo.setSignature(dto.getSignature());
                    vo.setDetail(tdUserInfo);

                    vo.setList(tdBlackDao.findUserIndexBlackList(1, user_id));
                    vo.setMark("0");
                    vo.setTip("成功");
                } else {
                    vo.setMark("1");
                    vo.setTip("该用户未实名认证");
                }

            } else {
                vo.setMark("1");
                vo.setTip("用户不存在");
            }


        } catch (Exception e) {
            vo.setMark("-1");
            vo.setTip("json数据错误");
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return vo;
    }
    /**
     * @Purpose:    申请实名认证
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo insertTdUserInfo(TdUserInfoInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertTdUserInfo:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict tsJzDict = tsJzDictDao.findDictById(dto.getProfession_id());
            if (tsJzDict != null && tsJzDict.getCode_type().equals("profession")) {
                TsJzDict province = tsJzDictDao.findDictById(dto.getProvince_id());
                if (province != null && province.getCode_type().equals("province")) {
                    TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
                    if (city != null && city.getParent_id() == province.getDict_id()) {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TdUserInfo tdUserInfo = tdUserInfoDao.findTdUserInfoById(user.getUser_id());
                        if (tdUserInfo != null) {
                            if (tdUserInfo.getState() == 1) {
                                retInfo.setMark("1");
                                retInfo.setTip("认证中");
                            } else if (tdUserInfo.getState() == 2) {
                                retInfo.setMark("1");
                                retInfo.setTip("已认证");
                            } else {
                                tdUserInfo.setCity_id(dto.getCity_id());
                                tdUserInfo.setHometown(province.getCode_name() + city.getCode_name());
                                tdUserInfo.setId_no(dto.getId_no());
                                tdUserInfo.setNick_name(dto.getNick_name());
                                tdUserInfo.setProfession(tsJzDict.getCode_name());
                                tdUserInfo.setProfession_id(dto.getProfession_id());
                                tdUserInfo.setProvince_id(dto.getProvince_id());
                                tdUserInfo.setReal_name(dto.getReal_name());
                                tdUserInfo.setSex(dto.getSex());
                                tdUserInfo.setSignature(dto.getSignature());
                                tdUserInfoDao.resubmitTdUserInfo(tdUserInfo);
                                retInfo.setMark("0");
                                retInfo.setTip("发起认证成功");
                            }
                        } else {
                            tdUserInfo = new TdUserInfo();
                            tdUserInfo.setCity_id(dto.getCity_id());
                            tdUserInfo.setHometown(province.getCode_name() + city.getCode_name());
                            tdUserInfo.setId_no(dto.getId_no());
                            tdUserInfo.setNick_name(dto.getNick_name());
                            tdUserInfo.setProfession(tsJzDict.getCode_name());
                            tdUserInfo.setProfession_id(dto.getProfession_id());
                            tdUserInfo.setProvince_id(dto.getProvince_id());
                            tdUserInfo.setReal_name(dto.getReal_name());
                            tdUserInfo.setSex(dto.getSex());
                            tdUserInfo.setUser_id(user.getUser_id());
                            tdUserInfo.setSignature(dto.getSignature());
                            tdUserInfoDao.insertTdUserInfo(tdUserInfo);
                            retInfo.setMark("0");
                            retInfo.setTip("发起认证成功");
                        }
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("市不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("省不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("职业不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    资料修改
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public  ResultVo updateTdUserInfo(TdUserInfoUpdateDto dto, String token) {
        String logInfo = this.getClass().getName() + ":updateTdUserInfo:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {

            TsJzDict tsJzDict = tsJzDictDao.findDictById(dto.getProfession_id());
            if (tsJzDict != null && tsJzDict.getCode_type().equals("profession")) {
                TsJzDict province = tsJzDictDao.findDictById(dto.getProvince_id());
                if (province != null && province.getCode_type().equals("province")) {
                    TsJzDict city = tsJzDictDao.findDictById(dto.getCity_id());
                    if (city != null && city.getParent_id() == province.getDict_id()) {
                        User user = (User)redisTemplate.opsForValue().get(token);
                        TdUserInfo tdUserInfo = tdUserInfoDao.findTdUserInfoById(user.getUser_id());
                        tdUserInfo.setCity_id(dto.getCity_id());
                        tdUserInfo.setHometown(province.getCode_name() + city.getCode_name());
                        tdUserInfo.setNick_name(dto.getNick_name());
                        tdUserInfo.setProfession(tsJzDict.getCode_name());
                        tdUserInfo.setProfession_id(dto.getProfession_id());
                        tdUserInfo.setProvince_id(dto.getProvince_id());
                        tdUserInfo.setSex(dto.getSex());
                        tdUserInfo.setSignature(dto.getSignature());
                        tdUserInfoDao.updateTdUserInfo(tdUserInfo);
                        user.setNick_name(tdUserInfo.getNick_name());
                        userDao.updateUserNick(user);
                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("市不存在");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("省不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("职业不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    获取用户资料
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdUserInfoDetailVo
     */
    public TdUserInfoDetailVo userInfoDetail(String token) {
        String logInfo = this.getClass().getName() + ":userInfoDetail:";
        TdUserInfoDetailVo retInfo = new TdUserInfoDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            TdUserInfo tdUserInfo = tdUserInfoDao.findTdUserInfoById(user.getUser_id());
            if (tdUserInfo != null && tdUserInfo.getState() == 2) {
                retInfo.setMark("0");
                retInfo.setTip("成功");


                retInfo.setAudit_remark(tdUserInfo.getAudit_remark());
                retInfo.setCity_id(tdUserInfo.getCity_id());
                retInfo.setHometown(tdUserInfo.getHometown());
                retInfo.setId_no(tdUserInfo.getId_no());
                retInfo.setNick_name(tdUserInfo.getNick_name());
                retInfo.setProfession(tdUserInfo.getProfession());
                retInfo.setProfession_id(tdUserInfo.getProfession_id());
                retInfo.setProvince_id(tdUserInfo.getProvince_id());
                retInfo.setReal_name(tdUserInfo.getReal_name());
                retInfo.setSex(tdUserInfo.getSex());
                retInfo.setState(tdUserInfo.getState());
                retInfo.setSignature(tdUserInfo.getSignature());
            } else {
                retInfo.setMark("1");
                retInfo.setTip("未认证成功");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
